﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import flash.display.*;
    import flash.geom.*;
    import plugin.*;

    public class AnimatedAsset extends AnimatedBitmapRoundRobbin {

        public static var s_cached:uint = 0;
        public static var s_total:uint = 0;
        public static var s_frame:uint = 0;
        public static var s_pixelSaved:uint = 0;
        public static var s_pixelUsed:uint = 0;
        private static var maxZoom:int = Global.gameSettings.getNumber("maxZoom", 4);

        private var bDisable:Boolean = false;
        protected var m_bitmaps:Array = null;
        protected var m_halfBitmaps:Array = null;
        protected var m_frameSkipCount:int = 0;
        protected var m_assetLocator:String = "";
        protected var m_useAllFrames:Boolean = false;

        public function AnimatedAsset(_arg1:String, _arg2:BitmapData, _arg3:int, _arg4:int, _arg5:int, _arg6:Number, _arg7:Array=null, _arg8:Array=null){
            var _local9:BitmapData;
            s_total++;
            this.m_assetLocator = _arg1;
            super(null, _arg3, _arg4, _arg5, _arg6);
            if ((((_arg7 == null)) || ((_arg8 == null)))){
                this.cacheBitmaps(_arg2, _arg6);
            } else {
                this.m_bitmaps = _arg7;
                this.m_halfBitmaps = _arg8;
                if (this.m_bitmaps.length > 0){
                    _local9 = this.m_bitmaps[0];
                    m_targetBitmapData = _local9;
                    bitmapData = _local9;
                    scaleX = 1;
                    scaleY = 1;
                };
            };
        }
        public static function printStats():void{
            var _local1:ConsoleStub = ConsoleStub.getInstance();
            if (_local1 != null){
                _local1.print("Asset", ((((((((("[AnimatedAsset] Frames = " + s_frame) + ", Sheets = ") + int(((s_pixelSaved * 4) / 0x0400))) + "KB, Sprites = ") + int(((s_pixelUsed * 4) / 0x0400))) + "KB, Hit = ") + s_total) + "/") + s_cached), 1);
            };
        }

        public function duplicate(_arg1:Number):AnimatedAsset{
            return (new AnimatedAsset(this.m_assetLocator, null, numFrames, m_frameWidth, m_frameHeight, _arg1, this.m_bitmaps, this.m_halfBitmaps));
        }
        public function set enable(_arg1:Boolean):void{
            this.bDisable = !(_arg1);
        }
        protected function cacheBitmaps(_arg1:BitmapData, _arg2:Number):void{
            var _local10:BitmapData;
            if (this.numFrames < 1){
                this.m_bitmaps = [];
                this.m_halfBitmaps = [];
                return;
            };
            this.m_bitmaps = new Array();
            this.m_halfBitmaps = new Array();
            if (!_arg1){
                return;
            };
            var _local3:int = Math.floor((_arg1.width / m_frameWidth));
            var _local4:int;
            while (_local4 < numFrames) {
                m_frameRect.x = ((_local4 % _local3) * m_frameWidth);
                m_frameRect.y = (Math.floor((_local4 / _local3)) * m_frameHeight);
                if (_local4 == 0){
                    _local10 = m_targetBitmapData;
                } else {
                    _local10 = new BitmapData(m_frameWidth, m_frameHeight, true, 0);
                };
                s_frame++;
                _local10.copyPixels(_arg1, m_frameRect, m_destPoint);
                this.m_bitmaps.push(_local10);
                _local4++;
            };
            var _local5:Bitmap = new Bitmap();
            var _local6:ColorTransform;
            var _local7:Number = Math.floor((m_frameWidth / 2));
            var _local8:Number = Math.floor((m_frameHeight / 2));
            var _local9:Matrix = new Matrix();
            _local9.scale((_local7 / m_frameWidth), (_local8 / m_frameHeight));
            if ((((_arg2 < 10)) && ((numFrames < 4)))){
                this.m_useAllFrames = true;
            };
            _local4 = 0;
            while (_local4 < numFrames) {
                _local10 = new BitmapData((m_frameWidth / 2), (m_frameHeight / 2), true, 0);
                s_frame++;
                _local5.bitmapData = this.m_bitmaps[_local4];
                _local10.draw(_local5, _local9, _local6, null, null, true);
                this.m_halfBitmaps.push(_local10);
                if (!this.m_useAllFrames){
                    _local4++;
                };
                _local4++;
            };
            if (this.m_bitmaps){
                _local10 = this.m_bitmaps[0];
                if (_local10){
                    m_targetBitmapData = _local10;
                    this.bitmapData = _local10;
                    this.scaleX = 1;
                    this.scaleY = 1;
                };
            };
            s_pixelSaved = (s_pixelSaved + (_arg1.width * _arg1.height));
            s_pixelUsed = (s_pixelUsed + int((((m_frameWidth * m_frameHeight) * numFrames) * 1.25)));
            s_cached++;
        }
        override protected function draw():void{
            var _local1:BitmapData;
            var _local2:Boolean;
            var _local3:int;
            var _local4:int;
            if (this.m_bitmaps == null){
                return;
            };
            if (!this.bDisable){
                this.m_frameSkipCount--;
                if (this.m_frameSkipCount > 0){
                    return;
                };
                this.m_frameSkipCount = 0;
                _local2 = (GlobalEngine.viewport.getZoom() < maxZoom);
                _local3 = Global.world.defCon;
                if (_local3 >= GameWorld.DEFCON_LEVEL2){
                    if (_local3 >= GameWorld.DEFCON_LEVEL3){
                        this.m_frameSkipCount = 4;
                    } else {
                        this.m_frameSkipCount = 2;
                    };
                };
                if (this.m_useAllFrames){
                    this.m_frameSkipCount = 0;
                };
                if (_local2){
                    _local4 = currentFrame;
                    if (!this.m_useAllFrames){
                        _local4 = (_local4 / 2);
                    };
                    _local1 = this.m_halfBitmaps[_local4];
                    if (_local1){
                        m_targetBitmapData = _local1;
                        this.bitmapData = _local1;
                        this.scaleX = 2;
                        this.scaleY = 2;
                    };
                    return;
                };
            };
            _local1 = this.m_bitmaps[currentFrame];
            if (_local1){
                m_targetBitmapData = _local1;
                this.bitmapData = _local1;
                this.scaleX = 1;
                this.scaleY = 1;
            };
        }

    }
}//package Classes 
